package uk.ac.aber.Blockmation;

import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import uk.ac.aber.Blockmation.Actions.ActionFrameBestFit;
import uk.ac.aber.Blockmation.Actions.ActionOvalTool;
import uk.ac.aber.Blockmation.Actions.ActionPointTool;
import uk.ac.aber.Blockmation.Actions.ActionRectangleTool;


public class LeftPanel extends JPanel implements ChangeListener
{
    private MainFrame mainFrame; //?
    private JLabel shapesLabel;
    private JButton ovalShapeButton;
    private JButton rectangleShapeButton;
    private JButton pointShapeButton;
    private JLabel chooseColourForLabel;
    private JButton backgroundColourButton;
    private JLabel blockSizeLabel;
    private JSpinner blockSizeSpinner;
    private JLabel    spinnerLabel;
    private JButton bestFitBlocksize;

    public LeftPanel(MainFrame mainFrame)
    {
        this.mainFrame = mainFrame;
        shapesLabel = new JLabel("Shapes");
        
        chooseColourForLabel = new JLabel("Choose Colour");
        blockSizeLabel = new JLabel("Block Size");
        
        ActionOvalTool actionOvalTool = new ActionOvalTool(mainFrame);
        actionOvalTool.setImage();
        ovalShapeButton = new JButton(actionOvalTool);
        
        ActionRectangleTool actionRectangleTool = new ActionRectangleTool(mainFrame);
        actionRectangleTool.setImage();
        rectangleShapeButton = new JButton(actionRectangleTool);
        
        ActionPointTool actionPointTool = new ActionPointTool(mainFrame);
        actionPointTool.setImage();
        pointShapeButton = new JButton(actionPointTool);

        spinnerLabel = new JLabel("Block size");
        spinnerLabel.setAlignmentX(LEFT_ALIGNMENT);
        


      //set size of buttons
        ovalShapeButton.setMaximumSize(new Dimension(30, 30));         // /w,l
        rectangleShapeButton.setMaximumSize(new Dimension(30, 30));    // /w,l
        pointShapeButton.setMaximumSize(new Dimension(30, 30));      // /w,l
        ovalShapeButton.setToolTipText("This button allows you to draw with ovals.");
        rectangleShapeButton.setToolTipText("This button allows you to draw with rectangles.");
        pointShapeButton.setToolTipText("This button allows you to draw points.");
        

        /*
        * Builds a spinner for setting block size.
        */
        int initial = mainFrame.getPad().getBlockSize().width;
        SpinnerNumberModel blockSizeSpinnerModel = new SpinnerNumberModel(initial, //initial value
                1, //minimal value
                150, //maximal value
                1); //step
        blockSizeSpinner = new JSpinner(blockSizeSpinnerModel);
        blockSizeSpinner.addChangeListener(this);
        //blockSizeSpinner.setPreferredSize(new Dimension(0,20));
        blockSizeSpinner.setAlignmentX(LEFT_ALIGNMENT);
        ///this.setBackground(Color.red);
        this.setBorder(BorderFactory.createTitledBorder("Tools"));

        
        
        
//      ///label for shapes
        JPanel toolbox = new JPanel();
        toolbox.setLayout(new BoxLayout(toolbox, BoxLayout.Y_AXIS));
        toolbox.add(Box.createRigidArea(new Dimension(0, 10)));
        toolbox.add(shapesLabel);
        toolbox.add(Box.createRigidArea(new Dimension(0, 10)));
        toolbox.add(ovalShapeButton);
        toolbox.add(Box.createRigidArea(new Dimension(0, 5)));
        toolbox.add(rectangleShapeButton);
        toolbox.add(Box.createRigidArea(new Dimension(0, 5)));
        toolbox.add(pointShapeButton);
        this.add(toolbox);

//      /////// layout the spinner bits
        JPanel toolbox4 = new JPanel();

        //toolbox4.setAlignmentX(toolbox4.LEFT_ALIGNMENT);
        BoxLayout boxLayout = new BoxLayout(toolbox4, BoxLayout.PAGE_AXIS);
        toolbox4.setLayout(boxLayout);

        ActionFrameBestFit actionFrameBestFit = new ActionFrameBestFit(mainFrame);
        bestFitBlocksize = new JButton(actionFrameBestFit);
        bestFitBlocksize.setAlignmentX(LEFT_ALIGNMENT);
        
        
//      /spinner
        toolbox4.add(Box.createRigidArea(new Dimension(0, 30)));
        toolbox4.add(spinnerLabel);
        toolbox4.add(Box.createRigidArea(new Dimension(0, 10)));
        toolbox4.add(blockSizeSpinner);
        toolbox4.add(Box.createRigidArea(new Dimension(0, 5)));
        toolbox4.add(bestFitBlocksize);
        this.add(toolbox4);
        this.setVisible(true);
    }


    public void stateChanged(ChangeEvent arg0) {
            int blockSize = Integer.parseInt(this.blockSizeSpinner.getModel().getValue().toString());
            mainFrame.getPad().setBlockSize(new Dimension(blockSize, blockSize));
            mainFrame.getDrawingSpace().revalidate();
    }


	public JSpinner getBlockSizeSpinner() {
		return blockSizeSpinner;
	}
}
